我自己建立了一个helloSbt,来看一下构建定义应该怎么来写。
build.sbt 的代码如下
\*这是注释*\
//这也是注释
lazy val root = (project in file("."))
.settings(
//name := "helloSBT",
name := baseDirectory.value.getName,
version := "1.0",
scalaVersion := "2.11.4"
)
.settings(
hello := { println(name.value + ", this is a task") }
)
lazy val hello = taskKey[Unit]("An example task")
organization := name.value
name in Compile := "hello_in_compile"
name in packageBin := "hello in task"
sourceDirectories in Compile += file("source")
sourceDirectories in Compile ++= Seq(file("sources1"), file("sources2"))
不同类型的Key,是不能放在一个Setting中的,不同的Setting[T],对应不同类型的Key
在没有定义项目时,sbt 会在构建中创建一个默认的项目并将其他项目也聚合起来,即root项目
custom key
hello 部分展示了如何定义一个custom task key, 首先 custom Key的定义,可以在文档的任意一个位置
其次,如果print的是 name
,而不是name.value
,则返回的是:
sbt.SettingKey$$anon$4@c2bffed
即name这个Key的id
扩展 Key 值
关于 sourceDirectories
的部分,可利用 +=
和 ++=
来添加额外的源代码路径
设置多个subprojects
如果要为多个projects设置,又需要为它们设置公共属性,可如此例:
lazy val commonSettings = Seq(
organization := "com.example",
version := "0.1.0",
scalaVersion := "2.12.3"
)
lazy val core = (project in file("core"))
.settings(
commonSettings,
// other settings
)
lazy val util = (project in file("util"))
.settings(
commonSettings,
// other settings
)
若需要设置项目间的依赖关系,来确定编译顺序,可通过设置 dependsOn
方法
lazy val core = project.dependsOn(util)